<template>
	<view class="root-view">
		<view class="scan-mod" v-if="true">
			<view class="scan-mhead">
				<view class="item-icon">
					<radar colorName="aqua" :height="160" :width="160"></radar>
				</view>
				<view class="item-lab"> 正在扫描设备 </view>
			</view>
			<view class="scan-mfoot">
				<button class="coco-btn gray xl round" @click="cancelBluetooth">取消</button>
			</view>
		</view>
		
		<view class="bluetooth-mod" v-if="true">
			<view class="bluetooth-view">
				<view class="bluetooth-cell" v-for="(item,index) of buleList" :key="index">
					<view class="bluetooth-item">
						<view class="item-head">
							<u-icon :name="$IMG_URL + '/imgs/icons/ic-bluetooth.png'" size="64"></u-icon>
						</view>
						<view class="item-body">
							<view class="item-title">{{item.name}}</view>
						</view>
						<view class="item-foot">
							<button class="coco-btn theme round shadow sm" @click="linkBlue(item)">连接</button>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import radar from '@/pages/device/components/radar.vue';
	export default {
		components:{
			radar
		},
		data() {
			return {
				buleList:[]
			};
		},
		onLoad() {
			//开启蓝牙
			uni.openBluetoothAdapter({
				fail:(res) => {
					uni.showModal({
						title:"系统提醒",
						content:res.errMsg
					})
				},
				success: (ress) => {
					uni.startBluetoothDevicesDiscovery({
						success: (bres) => {
							uni.onBluetoothDeviceFound((devices) => {
								console.log(devices);
								if(devices['devices'][0]['name']){
									this.buleList.push(devices['devices'][0]);
								}
							});
						}
					});
				}
			})
		},
		methods:{
			cancelBluetooth(){
				uni.showModal({
					title:"系统提醒",
					content:'确定要取消吗？',
					success: (res) => {
						if(res.confirm){
							uni.stopBluetoothDevicesDiscovery();
							uni.closeBluetoothAdapter();
							setTimeout(()=>{
								this.$Router.back();
							},1000)
						}
					}
				})	
			},
			linkBlue(bitem){
				let deviceId = bitem.deviceId;
				uni.createBLEConnection({
					deviceId:deviceId,
					success: (res) => {
						uni.stopBluetoothDevicesDiscovery();
						uni.showModal({
							title:"系统提醒",
							content:'重置网络成功，开始进入重新配网模式',
							showCancel:false,
							success: (res) => {
								if(res.confirm){
									uni.redirectTo({
										url:"/pages/device/add-device-step2"
									})
								}
							}
						})	
					},
					fail:(fres) => {
						uni.stopBluetoothDevicesDiscovery();
						console.log(fres);
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: auto;
		background-color: #fff;
	}
	.scan-mod{
		padding:40upx 14upx;
		box-sizing: border-box;
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		font-size: 28upx;
		.scan-mhead{
			flex: 1;
			display: flex;
			flex-direction: column;
			.item-icon{
				// padding-top: 10vh;
			}
			.item-lab{
				margin-top: 40upx;
				text-align: center;
				color: #c0c4cc;
			}
		}
		.scan-mfoot{
			margin-top:40upx;
			padding: 0 40upx;
		}
	}
	
	.bluetooth-mod{
		
	}
	.bluetooth-view{
		
	}
	.bluetooth-cell{
		position: relative;
		&::after{
			content:" ";
			position: absolute;
			left: 40upx;
			right: 40upx;
			bottom: 0;
			height: 1px;
			background-color: #F3F4F6;
			transform: scaleY(.5);
		}
	}
	.bluetooth-item{
		padding: 40upx 40upx;
		display: flex;
		align-items: center;
		background-color: #fff;
		border-radius: 16upx;
		font-size: 32upx;
		line-height: 1.2;
		.item-head{
			display: flex;
			align-items: center;
			margin-right: 24upx;
		}
		.item-body{
			flex: 1;
			min-width: 0;
		}
		.item-foot{
			margin-left: 24upx;
			text-align: right;
		}
		.item-title{
			
		}
	}
</style>
